import { createRouter, createWebHashHistory } from 'vue-router'
import { LoginRoutes } from '@/views/Login/routes'
import { ErrorsRoutes } from '@/views/Errors/routes'
import HeaderpartVue from "@/components/Headerpart.vue";
import FooterpartVue from "@/components/Footerpart.vue";
import HomePageVue from '@/views/Home/HomePage.vue';
import UserCenterVue from '@/views/Home/UserCenter.vue';
import GoodsCarVue from '@/views/Goods/GoodsCar.vue';
import GoodsListVue from '@/views/Goods/GoodsList.vue';
import OrderPageVue from '@/views/Home/OrderPage.vue';
import SetPage from '@/views/Home/SetPage.vue'
import AddressPage from '@/views/Home/AddressPage.vue'
import ProductDetail from '@/views/Home/ProductDetail.vue';
import PayFor from '@/views/Home/PayFor.vue';
import AddAdress from '@/views/Home/AddAdress.vue';
import ProductClassification from '@/views/Home/ProductClassification.vue'
import AdressEdit from '@/views/Home/AdressEdit.vue'
import SearchPage from '@/views/Goods/SearchPage.vue'
import AllorderPageVue from '@/views/Home/AllorderPage.vue';
import GoodsCommon from '@/views/Goods/GoodsCommon.vue'

const routes: any = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/order',
    name: 'order',
    component: OrderPageVue
  },
  {
    path:'/commonon',
    name:'commonon',
    component:GoodsCommon
  },
  {
    path: '/allorder',
    name: 'allorder',
    component: AllorderPageVue
  },
  {
    path: '/pay',
    name: 'pay',
    component: PayFor
  },
  {
    path: '/set',
    name: 'set',
    component: SetPage
  },
  {
    path: '/addres',
    name: 'addres',
    component: AddressPage
  },
  {
    path: '/search',
    name: 'search',
    component: SearchPage
  },
  {
    path: '/addadres',
    name: 'addadres',
    component: AddAdress
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '404'
  },
  ...LoginRoutes,
  ...ErrorsRoutes,
  {
    path: '/guide',
    name: 'guide',
    component: () => import('@/views/Guide/GuidePage.vue')
  },
  {
    path: "/home",
    // component: HomeView, 一个路径对应一个组件
    components: {
      default: HomePageVue,
      header: HeaderpartVue,
      footer: FooterpartVue,
    },
  },
  {
    path: '/detail',
    name: 'detail',
    component: ProductDetail
  },
  {
    path: '/user',
    name: 'user',
    components: {
      default: UserCenterVue,
      footer: FooterpartVue
    }
  },
  {
    path: '/car',
    name: 'car',
    components: {
      default: GoodsCarVue,
      footer: FooterpartVue
    }
  },
  {
    path: '/pdcclass',
    component: ProductClassification,
    name: 'pdcclass'
  },
  {
    path: '/adedit',
    name: 'adedit',
    component: AdressEdit
  },
  {
    path: '/class',
    name: 'class',
    components: {
      default: GoodsListVue,
      footer: FooterpartVue,
    },
  }
]


//3.创建路由
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

//路由守卫 跳转回之前页面
router.beforeEach((to, from, next) => {
  if ((to.fullPath == '/addres' && from.fullPath == '/car') || (to.fullPath == '/addres' && from.fullPath == '/user')) {
    localStorage.setItem('fullpath', from.fullPath)
  } else {
    localStorage.setItem('fullpath', '/car')
  }
  next()

})


export default router
